﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;

namespace AnyOffice.general.vehicle.manage
{
    public partial class _new : ModulePage
    {

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
       
        private bool IsImage(string contentType)
        {
            string str;
            return (((str = contentType) != null) && (((str == "image/gif") || (str == "image/jpeg")) || (str == "image/pjpeg")));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigManager manager = new ConfigManager();
            if (!this.Page.IsPostBack)
            {
                this.litImageMaxSize.Text = (manager.ImageUploadMaxSize / 1024).ToString();
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                if (this.filePhoto.PostedFile.ContentLength > 0)
                {
                    if (this.filePhoto.PostedFile.ContentLength > manager.ImageUploadMaxSize)
                    {
                        this.litMessage.Text = base.ShowPrompt("照片大小超过500k！");
                        return;
                    }
                    if (!this.IsImage(this.filePhoto.PostedFile.ContentType))
                    {
                        this.litMessage.Text = base.ShowPrompt("照片格式不正确！");
                        return;
                    }
                }
                VehicleDao dao = new VehicleDao();
                VehicleData data = new VehicleData();
                data.Number = this.txtNumber.Text;
                if (this.txtModel.Text != string.Empty)
                {
                    data.Model = this.txtModel.Text;
                }
                if (this.txtEngineNum.Text != string.Empty)
                {
                    data.EngineNum = this.txtEngineNum.Text;
                }
                data.Type = this.ddlType.SelectedValue;
                if (this.txtDriver.Text != string.Empty)
                {
                    data.Driver = this.txtDriver.Text;
                }
                if (this.txtPrice.Text != string.Empty)
                {
                    data.Price = this.txtPrice.Text;
                }
                if (this.txtBuyDate.Text != string.Empty)
                {
                    data.BuyDate = Convert.ToDateTime(this.txtBuyDate.Text);
                }
                data.Status = this.ddlStatus.SelectedValue;
                data.Remark = this.txtRemark.Text;
                if (this.filePhoto.PostedFile.ContentLength > 0)
                {
                    int contentLength = this.filePhoto.PostedFile.ContentLength;
                    byte[] buffer = new byte[contentLength];
                    this.filePhoto.PostedFile.InputStream.Read(buffer, 0, contentLength);
                    data.Photo = buffer;
                }
                dao.Insert(base.Conn, data);
                this.litMessage.Text = base.ShowPrompt("添加车辆成功！", "index1.aspx");
            }
        }
 

    }
}
